import Vue 			from 'vue'
import util 		from '../../util'
import axios 		from 'axios'
import { Toast } 	from 'vant'
Vue.use(Toast)

export default {
	name: 'realNameStatus',
	data(){
		return {
			realStatus:0,		//0:未认证  1:审核中  2:审核通过   3:审核驳回
			areaType:'',		//地区类型  1:国内   2:其他地区
			place:'',			//地区
			name:'',			//姓名
			IDtype:'',			//证件类型
			IDCard:'',			//证件号码
			serverWeiXin:'',	//客服微信
			token:'',
			ajaxBtn:false,		//页面是否渲染完毕
			hideBtn:false,		//是否隐藏底部按钮
		}
	},
	computed:{
		statusShow(){
			return this.realStatus===0?'未认证':this.realStatus===1?'实名认证中':this.realStatus===2?'认证审核通过':'认证审核失败';
		},
		statusDesc(){
			return this.realStatus===0?'您还未提交认证':this.realStatus===1?'信息已提交，请耐心等待审核':this.realStatus===2?'恭喜您，身份认证已通过':'很遗憾，身份认证失败';
		},
		btnStatus(){
			return this.realStatus===0?'未认证':this.realStatus===1?'审核中':this.realStatus===2?'开始交易':'重新认证';
		},
		isIphoneX(){
			return util.isIphoneX()
		},
		isIphone(){
			return util.client()
		},
		isMYKEY(){
			return util.isMYKEY()
		}
	},
	methods:{
		init(){
			//拉取实名认证状态
			this.render(util.getStorage('verifiedInfo'));
			if(!util.getStorage('verifiedInfo')){
				Toast.loading({
					message: '加载中...',
					duration:0,
					forbidClick: true
				});
			}
			util.fetch(util.ajaxUrl()+`/user/verified-info`,{
				token:this.token || util.getToken()
			}).then(res => {
				if(res.retCode === 0){
					Toast.clear();
					this.render(res.retData);
					util.setStorage('verifiedInfo',res.retData)
				}else{
					Toast(res.retMsg || '未知错误')
				}
			}).catch(error => {
				Toast.clear();
				console.log(error)
			})
			//拉取客服微信
			let otcPayConfig = util.getStorage('otcPayConfig');
			if(otcPayConfig){
				otcPayConfig = JSON.parse(otcPayConfig);
				this.serverWeiXin = otcPayConfig.kyc_update_serve_desc;
			}
			let params = {};
			if(this.isMYKEY) params.token = this.token || util.getToken();
			util.fetch(util.ajaxUrl()+`/mykey/otc-pay-config`,params).then(res => {
				if(res.retCode === 0){
					this.serverWeiXin = res.retData.kyc_update_serve_desc;
					util.setStorage('otcPayConfig',res.retData)
				}else{
					Toast(res.retMsg || '未知错误')
				}
			}).catch(error => {
				console.log(error)
			})
		},
		render(retData){
			//数据渲染
			if(!retData) return
			if(typeof retData === 'string') retData = JSON.parse(retData)
			this.areaType = retData.kyc_type;		//地区类型  1:国内   2:其他地区
			this.realStatus = retData.kyc_status;
			// this.realStatus = 3;
			this.place = retData.kyc_area;
			this.name = retData.name;
			this.IDtype = retData.kyc_certificate;
			this.IDCard = retData.id_no;
			this.ajaxBtn = true;
		},
		upload(){
			//点击按钮
			if(this.realStatus ===2){
				//开始交易
				this.goBack('special');
				// this.$router.go(-1)
			}else if(this.realStatus === 3){
				//重新认证
				this.$router.replace({
					path:'/goRealName',
					query:{
						death:1
					}
				})
			}
		},
		goBack(special){
			util.navigateBack(-1,this,special)
		}
	},
	created() {
		if(this.$route.query.hideBtn) this.hideBtn = true;
		util.waitSDKReady().then(token => {
			this.token = token;
			this.init();
		}).catch((e) => {
			Toast('获取token失败')
		})
		setTimeout(() => {
			//退出登录的时候native调用H5方法清除token缓存
			util.registerHandler('loginOutDeleteToken',res => {
				this.token = '';
				localStorage.removeItem('token');
			})
			util.callHandler('setStatusBarStyle',{
				style:'dark'
			})
		},500)
    }
}